package com.jandan.web.front;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.jandan.logic.JWordzFacade;
import com.jandan.ui.client.util.ClientUtil;
import com.jandan.ui.model.Account;
import com.jandan.util.EncryptUtil;

public class RegisterController implements Controller {
	private JWordzFacade jwordz;
	
	public void setJwordz(JWordzFacade jwordz) {
		this.jwordz = jwordz;
	}

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String name = request.getParameter("name");
		String province = request.getParameter("province");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		
		if(userName != null && userName.trim().length() >= 6 && password != null && password.trim().length() >= 6 && email != null){
			Account account = new Account();
			account.setUserName(userName);
			password = EncryptUtil.encryptPassword(password);
			account.setPassword(password);
			account.setEmail(email);
			account.setName(name);
			account.setAddress(address);
			account.setPhone(phone);
			account.setProvince(province);
			account.setType(ClientUtil.REGISTER);
			
			jwordz.insertAccount(account);
			return new ModelAndView("success");
		}
		
		return new ModelAndView("error");
	}
}
